<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Comment;
use App\Model\M9_good;

class CommentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex()
    {
        //
        $uid = session('user')->uid;
        $comment = Comment::where('gc_uid',$uid)->orderBy('gctime','desc')->get();
        foreach($comment as $k=>$v){
            $comment[$k]['good'] = M9_good::find($v['gc_gid']);
        }
        return view('home.comment',['title'=>'评论列表','comment'=>$comment]);
    }

    public function getAdd($gid)
    {
        //dd($gid);
        //$gid = $request['gid'];

        $good = M9_good::find($gid);

        return view('home.addcomment',['title'=>'发表评论','good'=>$good]);
    }

    public function postInsert(Request $request)
    {
        $data = $request->all();
        $uid = session('user')->uid;
        $gid = $request['gid'];
        $comment = new Comment;
        $comment->gc_uid = $uid;
        $comment->gc_gid = $gid;
        $comment->gcstatus = $data['gcstatus'];
        $comment->gccontent = $data['gccontent'];
        $comment->gctime = date('Y-m-d H:i:s');
        // dd($comment);
        if($comment->save()){
            return redirect('/home/comment/index')->with('success','评论成功');
        }else{
            return back()->with('error','评论失败，请重试');
        }
    }

}
